package com.bby.user.controller;

import cn.hutool.crypto.digest.MD5;
import cn.hutool.json.JSONObject;
import com.bby.common.exception.BBYException;
import com.bby.common.utils.R;
import com.bby.common.utils.StatusCode;
import com.bby.common.aop.ParseToken;
import com.bby.user.bean.req.*;
import com.bby.user.bean.LoginResult;
import com.bby.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户-用户相关
 */
@RestController
public class UserController {
    final int maxAge=30*60;
    private final UserService userService;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录
     * @description 登录通过则添加记录，返回用户信息和 token
     * @param params 表格信息
     */
    @PostMapping(value = "login")
    @ResponseBody
    public R login(HttpServletRequest request,@RequestBody LoginRequest params) throws BBYException {
        logger.debug("登录");
        logger.debug("params = " + params);
        logger.debug(request.getHeader("Content-Type"));
        if (ObjectUtils.isEmpty(params)){
            return R.error("参数错误");
        }
        MD5 md5= MD5.create();//将密码转为 md5
        params.setPassword(md5.digestHex(params.getPassword()));
        return R.ok(userService.login(params));
    }

    /**
     * 使用验证码登录
     * @param params 参数信息
     */
    @PostMapping(value = "loginByCaptcha")
    @ResponseBody
    public LoginResult loginByCaptcha(@RequestBody LoginByCaptchaRequest params) throws BBYException {
        logger.debug("登录:"+params);
        if (ObjectUtils.isEmpty(params)){
            throw new BBYException(StatusCode.NAME_ERROR,"用户名为空！");
        }
        return userService.loginByCaptcha(params);
    }
    /**
     * 登出
     */
    @RequestMapping("logout")
    public R logout(){
        logger.debug("登出");
        userService.logout();
        return R.ok();
    }

    /**
     * 用户注册
     * @param params 表格信息
     */
    @PostMapping(value = "register")
    @ResponseBody
    public R register(@RequestBody RegisterRequestParams params) throws BBYException {
        logger.debug("注册："+params);
        MD5 md5= MD5.create();//将密码转为 md5
        params.setPassword(md5.digestHex(params.getPassword()));
        return R.ok(userService.register(params));
    }



    @GetMapping(value = "test")
    @ParseToken
    public String doTest(String userId,HttpServletResponse response){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        logger.debug(request.getHeader("token"));
        logger.debug(userId);

        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        JSONObject jsonObject=new JSONObject();
        jsonObject.set("aaa","assasas");
        jsonObject.set("bbb","assasas");
        jsonObject.set("ccc",123);
        jsonObject.set("ddd",true);
//        JSONObject json = new JSONObject(captchaService.verifyCaptcha("1642081222586","cmugl"));
//        logger.info(json);
        return jsonObject.toString();
    }

}
